package com.houtai.service.company;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.TdCompany;
import com.houtai.bean.TsJzDict;
import com.houtai.bean.User;
import com.houtai.bean.dto.avtivity.works.TdCompanyIndexWorksDto;
import com.houtai.bean.dto.company.CompanyListDto;
import com.houtai.bean.dto.company.TdCompanyInsertDto;
import com.houtai.bean.dto.company.TdCompanyJobUpdateDto;
import com.houtai.bean.dto.company.TdCompanyUpdateDto;
import com.houtai.bean.vo.activity.works.TdActivityWorksList;
import com.houtai.bean.vo.activity.works.TdActivityWorksListVo;
import com.houtai.bean.vo.company.*;
import com.houtai.dao.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyService {
    private static Logger logger = Logger.getLogger(CompanyService.class);
    @Autowired
    private TdCompanyDao tdCompanyDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzDictDao tsJzDictDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private TdBlackDao tdBlackDao;
    @Autowired
    private TdActivityWorksDao tdActivityWorksDao;
    /**
     * @Purpose:    企业主页
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized TdCompanyJobDetailVo tdCompanyJobDetail(String token) {
        String logInfo =  ":tdCompanyJobDetail:";
        logger.info("======" + logInfo + "begin======");
        TdCompanyJobDetailVo vo = new TdCompanyJobDetailVo();
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
            if (tdCompany != null) {
                if (tdCompany.getState() == 2 ) {
                    vo.setDetail(tdCompanyDao.findTdTdCompanyJobDetailById(tdCompany.getCompany_id()));

                    vo.setMark("0");
                    vo.setTip("成功");
                } else {
                    vo.setMark("1");
                    vo.setTip("企业未认证");
                }
            } else {
                vo.setMark("1");
                vo.setTip("企业不存在");
            }



        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    企业主页作品列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdActivityWorksListVo
     */
    public TdActivityWorksListVo companyWorksList(TdCompanyIndexWorksDto dto) {
        String logInfo = this.getClass().getName() + ":companyWorksList:";
        TdActivityWorksListVo vo = new TdActivityWorksListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TdCompany tdCompany = tdCompanyDao.findTdCompanyById(dto.getCompany_id());
            if (tdCompany != null) {
                if (tdCompany.getState() == 2 ) {
                    String orderBy = "works_id DESC";
                    PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
                    List<TdActivityWorksList> lists = tdActivityWorksDao.findTdActivityWorksListByUserId(tdCompany.getUser_id());
                    PageInfo<TdActivityWorksList> list = new PageInfo<>(lists);
                    if (list.getTotal() > 0) {
                        vo.setCurrent_page(dto.getCurrent_page());
                        vo.setList(list.getList());
                        vo.setPage_size(list.getPageSize());
                        vo.setTotal_record(list.getTotal());
                        vo.setPages(list.getPages());
                        vo.setMark("0");
                        vo.setTip("查询成功");
                    } else {
                        vo.setMark("1");
                        vo.setTip("无作品");
                    }

                } else {
                    vo.setMark("1");
                    vo.setTip("企业未认证");
                }
            } else {
                vo.setMark("1");
                vo.setTip("企业不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            vo.setMark("-1");
            vo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    企业主页
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized TdCompanyIndexVo companyIndex(int company_id) {
        String logInfo =  ":companyIndex:";
        logger.info("======" + logInfo + "begin======");
        TdCompanyIndexVo vo = new TdCompanyIndexVo();
        try {
            TdCompany tdCompany = tdCompanyDao.findTdCompanyById(company_id);
            if (tdCompany != null) {
                if (tdCompany.getState() == 2 ) {
                    TdCompanyIndex tdCompanyIndex = new TdCompanyIndex();
                    tdCompanyIndex.setCompany_intro(tdCompany.getCompany_intro());
                    tdCompanyIndex.setCompany_label(tdCompany.getCompany_label());
                    tdCompanyIndex.setCompany_logo(tdCompany.getCompany_logo());
                    tdCompanyIndex.setContact(tdCompany.getContact());
                    tdCompanyIndex.setContact_address(tdCompany.getContact_address());
                    tdCompanyIndex.setLicense_no_img(tdCompany.getLicense_no_img());
                    tdCompanyIndex.setProvince_city_county(tdCompany.getProvince_city_county());
                    vo.setDetail(tdCompanyIndex);

                    vo.setList(tdBlackDao.findUserIndexBlackList(2, company_id));
                    vo.setMark("0");
                    vo.setTip("成功");
                } else {
                    vo.setMark("1");
                    vo.setTip("企业未认证");
                }
            } else {
                vo.setMark("1");
                vo.setTip("企业不存在");
            }



        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    园区企业列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public CompanyListVo companyList(CompanyListDto dto) {
        String logInfo = this.getClass().getName() + ":companyList:";
        CompanyListVo retInfo = new CompanyListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            String orderBy = "company_id DESC";

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<CompanyList> lists = tdCompanyDao.findCompanyList(dto);
            PageInfo<CompanyList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无企业信息");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    申请实名认证
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo insertTdCompany(TdCompanyInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":insertTdCompany:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TsJzDict province = tsJzDictDao.findDictById(dto.getProvince_id());
            if (province != null && province.getCode_type().equals("province")) {
                TsJzDict city = tsJzDictDao.findDictById(dto.getCity_id());
                if (city != null && city.getParent_id() == province.getDict_id()) {
                    TsJzDict county = tsJzDictDao.findDictById(dto.getCounty_id());
                    if (county != null && county.getParent_id() == city.getDict_id()) {
                        User user = (User)redisTemplate.opsForValue().get(token);
                        TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
                        if (tdCompany != null) {
                            if (tdCompany.getState() == 1) {
                                retInfo.setMark("1");
                                retInfo.setTip("认证中");
                            } else if (tdCompany.getState() == 2) {
                                retInfo.setMark("1");
                                retInfo.setTip("已认证");
                            } else {
                                tdCompany.setCity_id(dto.getCity_id());
                                tdCompany.setCampus_id(dto.getCampus_id());
                                tdCompany.setCity_id(dto.getCity_id());
                                tdCompany.setCompany_intro(dto.getCompany_intro());
                                tdCompany.setCompany_label(dto.getCompany_label());
                                tdCompany.setCompany_logo(dto.getCompany_logo());
                                tdCompany.setCompany_name(dto.getCompany_name());
                                tdCompany.setContact(dto.getContact());
                                tdCompany.setContact_address(dto.getContact_address());
                                tdCompany.setCounty_id(dto.getCounty_id());
                                tdCompany.setLicense_no(dto.getLicense_no());
                                tdCompany.setLicense_no_img(dto.getLicense_no_img());
                                tdCompany.setProvince_city_county(province.getCode_name() + city.getCode_name() + county.getCode_name());
                                tdCompany.setProvince_id(dto.getProvince_id());

                                tdCompanyDao.resubmitTdCompany(tdCompany);
                                retInfo.setMark("0");
                                retInfo.setTip("发起认证成功");
                            }
                        } else {
                            tdCompany = new TdCompany();
                            tdCompany.setCity_id(dto.getCity_id());
                            tdCompany.setCampus_id(dto.getCampus_id());
                            tdCompany.setCity_id(dto.getCity_id());
                            tdCompany.setCompany_intro(dto.getCompany_intro());
                            tdCompany.setCompany_label(dto.getCompany_label());
                            tdCompany.setCompany_logo(dto.getCompany_logo());
                            tdCompany.setCompany_name(dto.getCompany_name());
                            tdCompany.setContact(dto.getContact());
                            tdCompany.setContact_address(dto.getContact_address());
                            tdCompany.setCounty_id(dto.getCounty_id());
                            tdCompany.setLicense_no(dto.getLicense_no());
                            tdCompany.setLicense_no_img(dto.getLicense_no_img());
                            tdCompany.setProvince_city_county(province.getCode_name() + city.getCode_name() + county.getCode_name());
                            tdCompany.setProvince_id(dto.getProvince_id());
                            tdCompany.setUser_id(user.getUser_id());
                            tdCompanyDao.insertTdCompany(tdCompany);
                            retInfo.setMark("0");
                            retInfo.setTip("发起认证成功");
                        }
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("区不存在");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("市不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("省不存在");
            }
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    资料修改
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public  ResultVo updateTdCompany(TdCompanyUpdateDto dto, String token) {
        String logInfo = this.getClass().getName() + ":updateTdCompany:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsJzDict province = tsJzDictDao.findDictById(dto.getProvince_id());
            if (province != null && province.getCode_type().equals("province")) {
                TsJzDict city = tsJzDictDao.findDictById(dto.getCity_id());
                if (city != null && city.getParent_id() == province.getDict_id()) {
                    TsJzDict county = tsJzDictDao.findDictById(dto.getCounty_id());
                    if (county != null && county.getParent_id() == city.getDict_id()) {
                        User user = (User)redisTemplate.opsForValue().get(token);
                        TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
                        tdCompany.setCity_id(dto.getCity_id());
                        tdCompany.setCampus_id(dto.getCampus_id());
                        tdCompany.setCity_id(dto.getCity_id());
                        tdCompany.setCompany_intro(dto.getCompany_intro());
                        tdCompany.setCompany_label(dto.getCompany_label());
                        tdCompany.setCompany_logo(dto.getCompany_logo());

                        tdCompany.setContact(dto.getContact());
                        tdCompany.setContact_address(dto.getContact_address());
                        tdCompany.setCounty_id(dto.getCounty_id());

                        tdCompany.setProvince_city_county(province.getCode_name() + city.getCode_name() + county.getCode_name());
                        tdCompany.setProvince_id(dto.getProvince_id());
                        tdCompany.setUser_id(user.getUser_id());
                        tdCompanyDao.updateTdCompany(tdCompany);
                        retInfo.setMark("0");
                        retInfo.setTip("成功");
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("区不存在");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("市不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("省不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    获取用户资料
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdCompanyDetailVo
     */
    public TdCompanyDetailVo userInfoDetail(String token) {
        String logInfo = this.getClass().getName() + ":userInfoDetail:";
        TdCompanyDetailVo retInfo = new TdCompanyDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
            if (tdCompany != null && tdCompany.getState() == 2) {
                retInfo.setMark("0");
                retInfo.setTip("成功");
                retInfo.setAudit_remark(tdCompany.getAudit_remark());
                retInfo.setCampus_id(tdCompany.getCampus_id());
                retInfo.setCity_id(tdCompany.getCity_id());
                retInfo.setCompany_intro(tdCompany.getCompany_intro());
                retInfo.setCompany_label(tdCompany.getCompany_label());
                retInfo.setCompany_logo(tdCompany.getCompany_logo());
                retInfo.setContact(tdCompany.getContact());
                retInfo.setContact_address(tdCompany.getContact_address());
                retInfo.setCounty_id(tdCompany.getCounty_id());
                retInfo.setLicense_no(tdCompany.getLicense_no());
                retInfo.setLicense_no_img(tdCompany.getLicense_no_img());
                retInfo.setProvince_city_county(tdCompany.getProvince_city_county());
                retInfo.setProvince_id(tdCompany.getProvince_id());

            } else {
                retInfo.setMark("1");
                retInfo.setTip("未认证成功");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    资料修改
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public  ResultVo updateTdCompanyJob(TdCompanyJobUpdateDto dto, String token) {
        String logInfo = this.getClass().getName() + ":updateTdCompany:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsJzDict province = tsJzDictDao.findDictById(dto.getProvince_id());
            if (province != null && province.getCode_type().equals("province")) {
                TsJzDict city = tsJzDictDao.findDictById(dto.getCity_id());
                if (city != null && city.getParent_id() == province.getDict_id()) {
                    TsJzDict county = tsJzDictDao.findDictById(dto.getCounty_id());
                    if (county != null && county.getParent_id() == city.getDict_id()) {
                        TsJzDict industry = tsJzDictDao.findDictById(dto.getIndustry_id());
                        if (industry != null && industry.getCode_type().equals("industry")) {
                            TsJzDict company_people = tsJzDictDao.findDictById(dto.getCompany_people_id());
                            if (company_people != null && company_people.getCode_type().equals("company_people")) {
                                TsJzDict financing_scale = tsJzDictDao.findDictById(dto.getFinancing_scale_id());
                                if (financing_scale != null && financing_scale.getCode_type().equals("financing_scale")) {
                                    User user = (User)redisTemplate.opsForValue().get(token);
                                    TdCompany tdCompany = tdCompanyDao.findTdCompanyByUserId(user.getUser_id());
                                    tdCompany.setCity_id(dto.getCity_id());
                                    tdCompany.setCompany_intro(dto.getCompany_intro());
                                    tdCompany.setContact_address(dto.getContact_address());
                                    tdCompany.setCounty_id(dto.getCounty_id());
                                    tdCompany.setProvince_city_county(province.getCode_name() + city.getCode_name() + county.getCode_name());
                                    tdCompany.setProvince_id(dto.getProvince_id());
                                    tdCompany.setIndustry_id(dto.getIndustry_id());
                                    tdCompany.setIndustry(industry.getCode_name());
                                    tdCompany.setCompany_people_id(dto.getCompany_people_id());
                                    tdCompany.setCompany_people(company_people.getCode_name());
                                    tdCompany.setFinancing_scale_id(dto.getFinancing_scale_id());
                                    tdCompany.setFinancing_scale(financing_scale.getCode_name());

                                    tdCompanyDao.updateTdCompanyJob(tdCompany);
                                    retInfo.setMark("0");
                                    retInfo.setTip("成功");
                                } else {
                                    retInfo.setMark("1");
                                    retInfo.setTip("企业发展不存在");
                                }
                            } else {
                                retInfo.setMark("1");
                                retInfo.setTip("企业规模不存在");
                            }
                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("所属行业不存在");
                        }

                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("区不存在");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("市不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("省不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
